﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;



namespace BinAnalysisHelp
{
    public class BinHelp
    {
        private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
        //Current_Boot_Setting_Idx
        public static uint BootSettingIdx = 0x200014af;
        //当前出图的尺寸    0x200065c8[31:0] 当前出图的尺寸 16进制
        public static uint CurrentSize = 0x200065c8;

        // BinToTalLength  14c0 起始位置 +4 才是全部长度  
        public static uint BinToLength = 0x200014c0;

        public static uint Bin151 = 0x80500000;
        //flash的起始地址   +4 才是全部长度   这里需要跟固件地址保持更新
        public static uint FlashIdx = 0x2000026c;
        // Xmodem_RX_Data_Dest_Type的值写为6
        //public static uint Xmodem_RX_Data_Dest_Type = 0x20001329;


        /// <summary>
        /// Bin 全部解析文件
        /// </summary>
        public static List<BinBase> BinList;
        /// <summary>
        /// 连接关系地址解析 LoadSetting
        /// </summary>
        public static List<LoadSettingParse> lspList;

        /// <summary>
        /// 连接关系解析 Usb LoadSetting
        /// </summary>
        public static List<SettingFileIDInfo> usbList;
        /// <summary>
        /// 是否输出文本信息全局变量
        /// </summary>
        public static bool IsOutput = false;
        /// <summary>
        /// 是否开启分段烧写模式
        /// </summary>
        public static bool IsPartition = false;

        //private static List<int> updateIndex = null;
        /// <summary>
        /// 记录更新的索引  计算需要更新的Xmodem
        /// </summary>
        public static List<int> UpdateIndex
        {
            get; set;
        }



        public static List<BinBase> GetUpdateIndex()
        {

            if (null != UpdateIndex&& UpdateIndex.Count > 0 && IsPartition)
            {
                //根据UpdateIndex 最大值和最小值
                var max = UpdateIndex.Max();
                var min = UpdateIndex.Min();
                //根据最小值找到头文件
                var minL = ((int)(min / 4000)) * 0x1000;
                //根据最大值 找到尾文件
                var maxL = Math.Ceiling(max / 4000.0) * 0x1000;
                int length = Convert.ToInt32(maxL) - Convert.ToInt32(minL);
                var result = BinList.Skip(minL).Take(length).ToList();
                return result;
            }
            else {
                return BinList;
            }
           

        }



        /// <summary>
        /// 刷新记录索引
        /// </summary>
        public static void RefreshRecordIndex()
        {
            UpdateIndex = new List<int>();
        }
        /// <summary>
        /// 打开文件Bin
        /// </summary>
        /// <param name="path"></param>
        /// <param name="xclist"></param>
        public static void OpenBinFile(string path, List<xc151binparsing> xclist,bool isUsb = false)
        {
            BinList = BinToList(path);
            BinBaseToNode(BinList, xclist);
            BinList = BinBaseToIsp(BinList);
            SetModeIsp(ref BinList);
            if (ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSettings.Settings["IsSerial"].Value == "TVE")
            {
                SetLoadSetting(BinList);
            }
            else
            {
                var tlist = BinList.Where(c => !string.IsNullOrEmpty(c.note) && c.note.Contains("setting区1_31_")  && !c.note.Contains("_en") && !c.module.Contains("Header")).ToList();

                //using (StreamWriter writer = new StreamWriter($"{System.IO.Directory.GetCurrentDirectory() }\\Lib\\xc151_Bin\\BBusb_lspListsetting.txt"))
                //{
                //    for (int i = 0; i < tlist.Count(); i++)
                //    {
                //        writer.WriteLine($"{tlist[i].original.ToString("X2")}  {tlist[i].note}");
                //    }
                //}

                Setting1_31Parser s1_31 = new Setting1_31Parser();
                s1_31.LoadSettingData(tlist.Select(c => c.original).ToList());
                usbList = s1_31.GetFileIDInfo();
                //using (StreamWriter writer = new StreamWriter($"{System.IO.Directory.GetCurrentDirectory() }\\Lib\\xc151_Bin\\BBusblist_lspListsetting.txt"))
                //{
                    
                //    for (int i = 0; i < usbList.Count(); i++)
                //    {
                //        writer.WriteLine($"{usbList[i].ID} {usbList[i].Type.ToString()} {usbList[i].width } {usbList[i].height }");
                //    }
                //}


            }
            
           
        }

        public static List<string> shead;
        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static List<BinBase> BinToList(string path, bool isbool = false)
        {
            List<BinBase> blist = new List<BinBase>();
            try
            {
                var list = System.IO.File.ReadAllBytes(path).ToList();
                string fffd = "";
                string fffe = "";
                //string fff6 = "";
                //bool f6bool = false;
                //double fff7 = 0;
                //bool f7bool = false;
                //int bigsmall = -1; //-1表示默认  0表示big 1表示small
                string exdata = string.Empty;
                for (int i = 0; i < list.Count();)
                {
                    
                    if (i + 4 <= list.Count())
                    {
                        string bigaddrT = string.Empty;
                        string bigdataT = string.Empty;
                        string smalladdrT = string.Empty;
                        string smalldataT = string.Empty;
                        if (isbool)
                        {
                            bigaddrT = $"{list[i].ToString("X2")}{list[i + 1].ToString("X2")}";
                            bigdataT = $"{list[i + 2].ToString("X2")}{list[i + 3].ToString("X2")}";
                            smalladdrT = $"{list[i + 1].ToString("X2")}{list[i].ToString("X2")}";
                            smalldataT = $"{list[i + 3].ToString("X2")}{list[i + 2].ToString("X2")}";
                        }
                        else
                        {
                            bigaddrT = $"{list[i + 2].ToString("X2")}{list[i + 3].ToString("X2")}";
                            bigdataT = $"{list[i].ToString("X2")}{list[i + 1].ToString("X2")}";
                            smalladdrT = $"{list[i + 3].ToString("X2")}{list[i + 2].ToString("X2")}";
                            smalldataT = $"{list[i + 1].ToString("X2")}{list[i].ToString("X2")}";
                        }

                     
                            for (int j = 0; j < 4; j++)
                            {
                                BinBase model = new BinBase();
                                model.original = list[i];
                                model.bigaddr = bigaddrT;
                                model.smalladdr = smalladdrT;
                                model.bigdata = bigdataT;
                                model.smalldata = smalldataT;
                                model.sort = j;
                                model.AnalyticalModel = exdata;
                                model.AddrVal = j > 1 ? AddressValueEnum.Addr : AddressValueEnum.Val;
                                if (!string.IsNullOrEmpty(fffd) && !string.IsNullOrEmpty(fffe))
                                {
                                    model.addresspart = $"{Convert.ToUInt16(fffd, 16).ToString("X2")}{Convert.ToUInt16(fffe, 16).ToString("X2")}";
                                }
                                blist.Add(model);
                                i++;
                            }
                      

                    }
                    else { i++; }


                }


            }
            catch (Exception ex)
            {
                logger.Debug($"{ex.Message.ToString()}");
                //throw ex;
            }

            return blist;
        }
        /// <summary>
        /// 赋值bin属性值
        /// </summary>
        /// <param name="blist"></param>
        /// <param name="xclist"></param>
        /// <returns></returns>
        public static List<BinBase> BinBaseToNode(List<BinBase> blist, List<xc151binparsing> xclist)
        {
            int headKey = 0x368;
            int headLength = 0x320;
            //解析头文件
            var HeadStartAddr = Convert.ToUInt32("0x" + blist[headKey + 1].original.ToString("X2") + blist[headKey].original.ToString("X2"), 16);
            var HeadEndAddr = HeadStartAddr + headLength;
            int no = 0;
            for (int i = Convert.ToInt32(HeadStartAddr); i < HeadEndAddr; i++)
            {
                if (i >= HeadEndAddr - 4)
                {
                    for (int j = i; j < HeadEndAddr; j++)
                    {
                        blist[j].describe = "crc";
                        blist[j].remarks = "头文件crc";
                        blist[j].crcold = blist[i].original.ToString("X") + blist[i + 1].original.ToString("X") + blist[i + 2].original.ToString("X") + blist[i + 3].original.ToString("X");
                        blist[j].crchead = HeadStartAddr.ToString();
                        blist[j].crctail = (HeadEndAddr - 4).ToString();
                        blist[j].module = "Header";
                        blist[j].note = "Header_crc";
                        blist[i].section = "Header_crc";
                    }
                    i += 2;
                }
                else
                {
                    blist[i].address = xclist[no].address;
                    blist[i].describe = xclist[no].describe;
                    blist[i].dataexample = xclist[no].dataexample;
                    blist[i].module = xclist[no].module;
                    blist[i].section = xclist[no].section;
                    blist[i].note = xclist[no].note;
                    no++;
                }
            }

            var headList = blist.Where(c => c.module == "Header" && !string.IsNullOrEmpty(c.note) && !c.note.Contains("_en") && c.smalldata != "0000").ToList();
            shead = headList.Select(c => c.note).Distinct().ToList();
         
            for (int i = 0; i < shead.Count(); i++)
            {
                if (shead[i].ToLower().Contains("pu_ct")|| shead[i].ToLower().Contains("boot_log") || shead[i].ToLower().Contains("header_crc")) continue;
               
                var model = blist.FindLast(c => c.module == "Header" && c.note == shead[i] && !c.note.Contains("_crc"));
                int listAddr = 0;
                if (null != model)
                {
                    listAddr = Convert.ToInt32(model.smalldata, 16);
                }
                int listnextAddr = 0;
                if (i + 1 < shead.Count())
                {
                    var sData = blist.FindLast(c => c.module == "Header" && c.note == shead[i + 1] && !c.note.Contains("_crc"));
                    if (null == sData || sData.smalldata == "FFFF")
                    {
                        listnextAddr = blist.Count();
                    }
                    else
                    {
                        listnextAddr = Convert.ToInt32(sData.smalldata, 16);
                    }

                }
                if (listAddr > blist.Count())
                {
                    continue;
                }
                /*查找开始区域中Setting头文件  后四个字节进行数据解析 视频制式、格式、排序
                1.0 参照标准 FF F5 后面四个字节为解析数据
                2.0 对象中获取一个列表的范围，然后在这个范围内查找第一个满足条件（即 smalladdr 属性转换为小写后等于 "fff5"）的元素，并返回它的索引
                3.0  blist[indexT].bigaddr; 排序
               
                */
                string videoHex = string.Empty;
                string transferHex = string.Empty;
                string idxHex = string.Empty;

                if (shead[i].Contains("App setting区") && blist.GetRange(listAddr, listnextAddr - listAddr).Exists(c => c.smalladdr.ToLower() == "fff5"))
                {
                    var indexT = blist.GetRange(listAddr, listnextAddr - listAddr).FindIndex(c => c.smalladdr.ToLower() == "fff5") + listAddr + 4;

                    //videoHex = blist[indexT].bigdata.Substring(0, 2);
                    //transferHex = blist[indexT].bigdata.Substring(blist[indexT].bigdata.Length - 2);
                    //idxHex = blist[indexT].bigaddr.Substring(blist[indexT].bigaddr.Length - 2);
                }

                //var addrTT = addrT + listAddr;
                //每个区域赋值属性
                for (int j = listAddr; j < listnextAddr; j++)
                {
                    blist[j].module = model.section;
                    blist[j].note = model.note;
                    blist[j].VideoHex = videoHex;
                    blist[j].TransferHex = transferHex;
                    blist[j].IdxHex = idxHex;

                    if (blist[j].smalladdr.ToLower() == "fff5")
                    {
                        blist[j].describe = "crc";
                        blist[j].remarks = $"{model.note}_crc";
                        blist[j].crcold = blist[j].smalldata;
                        blist[j].crchead = listAddr.ToString();
                        blist[j].crctail = (listAddr + 8).ToString();
                        blist[j].crclength = "8";
                    }
                    else if (blist[j].bigaddr.ToLower() == "fff5")
                    {
                        blist[j].describe = "crc";
                        blist[j].remarks = $"{model.note}_crc";
                        blist[j].crcold = blist[j].bigdata;
                        blist[j].crchead = listAddr.ToString();
                        blist[j].crctail = (listAddr + 8).ToString();
                        blist[j].crclength = "8";
                    }
                    else if (blist[j].bigaddr.ToLower() == "f6f5") //修改f6f5 长度128 变成 256
                    {
                        blist[j].describe = "crc";
                        blist[j].remarks = $"{model.note}_crc";
                        blist[j].crcold = blist[j].bigdata;
                        blist[j].crchead = j.ToString();
                        blist[j].crctail = (listAddr + 256).ToString();
                        blist[j].crclength = "256";
                    }
                    else if (blist[j].smalladdr.ToLower() == "f6f5") //修改f6f5 长度128 变成 256
                    {
                        blist[j].describe = "crc";
                        blist[j].remarks = $"{model.note}_crc";
                        blist[j].crcold = blist[j].smalldata;
                        blist[j].crchead = j.ToString();
                        blist[j].crctail = (listAddr + 256).ToString();
                        blist[j].crclength = "256";
                    }
                }
            }
            //listT = list.Where(c => c.note != null && c.note.Contains(terms)).Distinct(new SmallBinBaseCompare()).ToList();
            if (IsOutput)
            {
                using (StreamWriter writer = new StreamWriter($"{System.IO.Directory.GetCurrentDirectory() }\\Lib\\xc151_Bin\\B_setting.txt"))
                {
                    for (int i = 0; i < blist.Count(); i++)
                    {
                        if (blist[i].sort == 0x3)
                        {
                            writer.WriteLine($"{blist[i].smallexample},//{blist[i].addresspart} --{blist[i].note} --{blist[i].remarks}--{ blist[i].@module}");
                        }
                    }
                }
            }
            return blist;

        }


        public static List<BinBase> BinBaseToIsp(List<BinBase> blist, bool isbool = false)
        {

            if (IsOutput)
            {
                using (StreamWriter writer = new StreamWriter($"{System.IO.Directory.GetCurrentDirectory() }\\Lib\\xc151_Bin\\A_BListSetting.txt"))
                {
                    for (int i = 0; i < blist.Count(); i++)
                    {
                            writer.WriteLine($"{blist[i].bigaddr}_{blist[i].smalladdr}_{blist[i].smalldata}_{blist[i].bigexample}_{blist[i].bigexample}_{blist[i].smallexample}_{blist[i].AnalyticalModel}_{blist[i].AddrVal}_{blist[i].addresspart}{blist[i].addrsmallex}");
                    }
                }
            }

            var startHeaderIndex = blist.FindIndex(c => null != c.note && c.note.Contains("setting区0_00_首地址"));
            var endHeaderIndex = blist.FindLastIndex(c => null != c.note && c.note.Contains("Header_crc"));
            var nums = endHeaderIndex - startHeaderIndex;
            var appENList = blist.Where(c => null != c.note && c.note.Contains("App setting区") && c.note.Contains("_en") && c.original == 1).Select(c => c.note.Replace("_en", "")).Distinct().ToList();
            var result = blist.Skip(startHeaderIndex).Take(nums).Where(c => null != c.note && c.sort == 0x3 && appENList.Any(b => c.note.Contains(b))).ToList();


            string fffd = "";
            string fffe = "";
            string fff6 = "";
            bool f6bool = false;
            double fff7 = 0;
            bool f7bool = false;
            int bigsmall = -1; //-1表示默认  0表示big 1表示small
            string exdata = string.Empty;
            foreach (var item in result)
            {
                var sdata = Convert.ToInt32(item.smalldata, 16);
                if (blist.Count <= sdata)
                {
                    continue;
                }
                var t = blist[sdata];
                var t1 = blist[sdata + 4];
                int count = 0;
                if (t.smalladdr == "5843" && t.smalldata == "0151" && t1.smalldata != "0000")
                {
                    count = Convert.ToInt32(t1.smalldata, 16) * 4;
                }
                //统计出需要解析的数据 
                var sResult = blist.Skip(sdata).Take(count).ToList();
                var scount = sdata + count;
                for (int i = sdata; i < sdata+count; )
                {
                    //新增setting 是否是文件内容  用来区分是不是setting文件内容
                    blist[i].IsSetting = true;
                    if (i + 4 <= scount)
                    {
                        string bigaddrT = string.Empty;
                        string bigdataT = string.Empty;
                        string smalladdrT = string.Empty;
                        string smalldataT = string.Empty;

                        if (isbool)
                        {
                            bigaddrT = $"{blist[i].original.ToString("X2")}{blist[i + 1].original.ToString("X2")}";
                            bigdataT = $"{blist[i + 2].original.ToString("X2")}{blist[i + 3].original.ToString("X2")}";
                            smalladdrT = $"{blist[i + 1].original.ToString("X2")}{blist[i].original.ToString("X2")}";
                            smalldataT = $"{blist[i + 3].original.ToString("X2")}{blist[i + 2].original.ToString("X2")}";
                        }
                        else
                        {
                            bigaddrT = $"{blist[i + 2].original.ToString("X2")}{blist[i + 3].original.ToString("X2")}";
                            bigdataT = $"{blist[i].original.ToString("X2")}{blist[i + 1].original.ToString("X2")}";
                            smalladdrT = $"{blist[i + 3].original.ToString("X2")}{blist[i + 2].original.ToString("X2")}";
                            smalldataT = $"{blist[i + 1].original.ToString("X2")}{blist[i].original.ToString("X2")}";
                        }

                        if (bigaddrT.ToLower() == "fffd" || smalladdrT.ToLower() == "fffd")
                        {
                            fffd = bigaddrT.ToLower() == "fffd" ? bigdataT : smalldataT;
                        }
                        else if (bigaddrT.ToLower() == "fffe" || smalladdrT.ToLower() == "fffe")
                        {
                            fffe = bigaddrT.ToLower() == "fffe" ? bigdataT : smalldataT;
                        }
                        else if (bigaddrT.ToLower() == "fff6" || smalladdrT.ToLower() == "fff6")
                        {
                            fff6 = bigaddrT.ToLower() == "fff6" ? bigdataT : smalldataT;
                            f6bool = true;
                            bigsmall = bigaddrT.ToLower() == "fff6" ? 0 : 1;
                        }
                        else if (bigaddrT.ToLower() == "fff5"|| smalladdrT.ToLower() == "fff5")
                        {
                            for (int j = 0; j < 4; j++)
                            {
                                blist[i].IsSetting = true;
                                blist[i].bigaddr = bigaddrT;
                                blist[i].smalladdr = smalladdrT;
                                blist[i].bigdata = bigdataT;
                                blist[i].smalldata = smalldataT;
                                blist[i].sort = j;
                                blist[i].AddrVal = j > 1 ? AddressValueEnum.Addr : AddressValueEnum.Val;
                                if (!string.IsNullOrEmpty(fffd) && !string.IsNullOrEmpty(fffe))
                                {
                                    blist[i].addresspart = $"{Convert.ToUInt16(fffd, 16).ToString("X2")}{Convert.ToUInt16(fffe, 16).ToString("X2")}";
                                }
                                i++;
                            }
                            continue;
                        }
                        else if (bigaddrT.ToLower() == "f6f5"|| smalladdrT.ToLower() == "f6f5")
                        {
                            for (int j = 0; j < 4; j++)
                            {
                                blist[i].IsSetting = true;
                                blist[i].bigaddr = bigaddrT;
                                blist[i].smalladdr = smalladdrT;
                                blist[i].bigdata = bigdataT;
                                blist[i].smalldata = smalldataT;
                                blist[i].sort = j;
                                blist[i].AddrVal = j > 1 ? AddressValueEnum.Addr : AddressValueEnum.Val;
                                if (!string.IsNullOrEmpty(fffd) && !string.IsNullOrEmpty(fffe))
                                {
                                    blist[i].addresspart = $"{Convert.ToUInt16(fffd, 16).ToString("X2")}{Convert.ToUInt16(fffe, 16).ToString("X2")}";
                                }
                                i++;
                            }
                            continue;
                        }


                        if (f6bool)
                        {
                            string bigaddrNT, bigdataNT, smalladdrNT, smalldataNT;
                            if (isbool)
                            {
                                bigaddrNT = $"{blist[i + 4].original.ToString("X2")}{blist[i + 1 + 4].original.ToString("X2")}";
                                bigdataNT = $"{blist[i + 2 + 4].original.ToString("X2")}{blist[i + 3 + 4].original.ToString("X2")}";
                                smalladdrNT = $"{blist[i + 1 + 4].original.ToString("X2")}{blist[i + 4].original.ToString("X2")}";
                                smalldataNT = $"{blist[i + 3 + 4].original.ToString("X2")}{blist[i + 2 + 4].original.ToString("X2")}";
                            }
                            else
                            {
                                bigaddrNT = $"{blist[i + 2 + 4].original.ToString("X2")}{blist[i + 3 + 4].original.ToString("X2")}";
                                bigdataNT = $"{blist[i + 4].original.ToString("X2")}{blist[i + 1 + 4].original.ToString("X2")}";
                                smalladdrNT = $"{blist[i + 3 + 4].original.ToString("X2")}{blist[i + 2 + 4].original.ToString("X2")}";
                                smalldataNT = $"{blist[i + 1 + 4].original.ToString("X2")}{blist[i + 4].original.ToString("X2")}";
                            }
                            if (bigaddrNT.ToLower() == "fff7" || smalladdrNT.ToLower() == "fff7")
                            {
                                fff7 = Convert.ToInt32(bigaddrNT.ToLower() == "fff7" ? bigdataNT : smalldataNT, 16);
                                f7bool = true;
                                f6bool = false;
                                bigsmall = bigaddrNT.ToLower() == "fff7" ? 0 : 1;
                            }

                            string example = string.Empty;
                            exdata = string.Empty;

                            if (bigsmall == 0)
                            {
                                switch (bigdataT.Substring(bigdataT.Length - 2))
                                {
                                   
                                    case "01":
                                    case "02":
                                        exdata = "03";
                                        break;
                                    case "03":
                                        exdata = "05";
                                        break;
                                    case "04":
                                        exdata = "06";
                                        break;
                                    case "05":
                                        exdata = "07";
                                        break;
                                    default:
                                        exdata = smalldataT.Substring(smalldataT.Length - 2);
                                        break;
                                }
                                example = $"0xfff8,0x{bigdataT.Substring(0, 2)},0xfffe,0x{exdata},";
                            }
                            else if (bigsmall == 1)
                            {
                                switch (smalldataT.Substring(smalldataT.Length - 2))
                                {

                                    case "01":
                                    case "02":
                                        exdata = "03";
                                        break;
                                    case "03":
                                        exdata = "05";
                                        break;
                                    case "04":
                                        exdata = "06";
                                        break;
                                    case "05":
                                        exdata = "07";
                                        break;
                                    default:
                                        exdata = smalldataT.Substring(smalldataT.Length - 2);
                                        break;
                                }
                                example = $"0xfff8,0x{smalldataT.Substring(0, 2)},\n0xfffe,0x{exdata},";
                            }

                            for (int j = 0; j < 4; j++)
                            {
                                blist[i].IsSetting = true;
                                blist[i].bigaddr = bigaddrT;
                                blist[i].smalladdr = smalladdrT;
                                blist[i].bigdata = bigdataT;
                                blist[i].smalldata = smalldataT;
                                blist[i].bigexample = example;
                                blist[i].smallexample = example;
                                blist[i].AnalyticalModel = exdata;
                                blist[i].sort = j;
                                blist[i].AddrVal = j > 1 ? AddressValueEnum.Addr : AddressValueEnum.Val;
                                if (!string.IsNullOrEmpty(fffd) && !string.IsNullOrEmpty(fffe))
                                {
                                    blist[i].addresspart = $"{Convert.ToUInt16(fffd, 16).ToString("X2")}{Convert.ToUInt16(fffe, 16).ToString("X2")}";
                                }
                                i++;
                            }
                           
                            for (int j = 0; j < 4; j++)
                            {
                                blist[i].IsSetting = true;
                                blist[i].bigaddr = bigaddrNT;
                                blist[i].smalladdr = smalladdrNT;
                                blist[i].bigdata = bigdataNT;
                                blist[i].smalldata = smalldataNT;
                                blist[i].AnalyticalModel = exdata;
                                blist[i].sort = j;
                                blist[i].AddrVal = j > 1 ? AddressValueEnum.Addr : AddressValueEnum.Val;
                                if (!string.IsNullOrEmpty(fffd) && !string.IsNullOrEmpty(fffe))
                                {
                                    blist[i].addresspart = $"{Convert.ToUInt16(fffd, 16).ToString("X2")}{Convert.ToUInt16(fffe, 16).ToString("X2")}";
                                }
                                i++;
                            }
                        }
                        else if (f7bool)
                        {
                            if (exdata == "00")
                            {
                                if (isbool)
                                {
                                    bigaddrT = $"{blist[i + 1].original.ToString("X2")}";
                                    bigdataT = $"{blist[i].original.ToString("X2")}";
                                    smalladdrT = $"{blist[i].original.ToString("X2")}";
                                    smalldataT = $"{blist[i + 1].original.ToString("X2")}";
                                }
                                else
                                {
                                    bigaddrT = $"{blist[i].original.ToString("X2")}";
                                    bigdataT = $"{blist[i + 1].original.ToString("X2")}";
                                    smalladdrT = $"{blist[i + 1].original.ToString("X2")}";
                                    smalldataT = $"{blist[i].original.ToString("X2")}";
                                }

                                for (int j = 0; j < 2; j++)
                                {
                                    blist[i].IsSetting = true;
                                    blist[i].bigaddr = bigaddrT;
                                    blist[i].smalladdr = smalladdrT;
                                    blist[i].bigdata = bigdataT;
                                    blist[i].smalldata = smalldataT;
                                    blist[i].sort = j;
                                    blist[i].AnalyticalModel = exdata;
                                    blist[i].AddrVal = j > 0 ? AddressValueEnum.Addr : AddressValueEnum.Val;
                                    if (!string.IsNullOrEmpty(fffd) && !string.IsNullOrEmpty(fffe))
                                    {
                                        blist[i].addresspart = $"{Convert.ToUInt16(fffd, 16).ToString("X2")}{Convert.ToUInt16(fffe, 16).ToString("X2")}";
                                    }
                                    i++;
                                }
                                fff7 -= 1;
                            }
                            else if (exdata == "03")
                            {
                                for (int j = 0; j < 4; j++)
                                {
                                    blist[i].IsSetting = true;
                                    blist[i].bigaddr = bigaddrT;
                                    blist[i].smalladdr = smalladdrT;
                                    blist[i].bigdata = bigdataT;
                                    blist[i].smalldata = smalldataT;
                                    blist[i].sort = j;
                                    blist[i].AnalyticalModel = exdata;
                                    blist[i].AddrVal = j > 1 ? AddressValueEnum.Addr : AddressValueEnum.Val;
                                    if (!string.IsNullOrEmpty(fffd) && !string.IsNullOrEmpty(fffe))
                                    {
                                        blist[i].addresspart = $"{Convert.ToUInt16(fffd, 16).ToString("X2")}{Convert.ToUInt16(fffe, 16).ToString("X2")}";
                                    }
                                    i++;
                                }
                                
                                fff7 -= 1;
                            }else
                            {
                                string bigaddrNT, bigdataNT, smalladdrNT, smalldataNT;
                                if (isbool)
                                {
                                    bigaddrNT = $"{blist[i + 4].original.ToString("X2")}{blist[i + 1 + 4].original.ToString("X2")}";
                                    bigdataNT = $"{blist[i + 2 + 4].original.ToString("X2")}{blist[i + 3 + 4].original.ToString("X2")}";
                                    smalladdrNT = $"{blist[i + 1 + 4].original.ToString("X2")}{blist[i + 4].original.ToString("X2")}";
                                    smalldataNT = $"{blist[i + 3 + 4].original.ToString("X2")}{blist[i + 2 + 4].original.ToString("X2")}";
                                }
                                else
                                {
                                    bigaddrNT = $"{blist[i + 2 + 4].original.ToString("X2")}{blist[i + 3 + 4].original.ToString("X2")}";
                                    bigdataNT = $"{blist[i + 4].original.ToString("X2")}{blist[i + 1 + 4].original.ToString("X2")}";
                                    smalladdrNT = $"{blist[i + 3 + 4].original.ToString("X2")}{blist[i + 2 + 4].original.ToString("X2")}";
                                    smalldataNT = $"{blist[i + 1 + 4].original.ToString("X2")}{blist[i + 4].original.ToString("X2")}";
                                }
                                string example = string.Empty;
                                string addrEx = string.Empty;

                                if (bigsmall == 0)
                                {
                                    addrEx = bigaddrT;
                                    example = $"0xffe0,0x{bigaddrT},\n0xffe1,0x{bigdataT},\n0xffe2,0x{bigaddrNT},\n0xffe3,0x{bigdataNT},";
                                }
                                else if (bigsmall == 1)
                                {
                                    addrEx = smalladdrT;
                                    example = $"0xffe0,0x{smalladdrT},\n0xffe1,0x{smalldataT},\n0xffe2,0x{smalladdrNT},\n0xffe3,0x{smalldataNT},";
                                }
                                for (int j = 0; j < 8; j++)
                                {
                                    blist[i].IsSetting = true;
                                    blist[i].bigaddr = bigdataT;
                                    blist[i].smalladdr = smalldataT;
                                    blist[i].bigdata = bigaddrNT + bigdataNT;
                                    blist[i].smalldata = smalladdrNT + smalldataNT;
                                    blist[i].addresspart = addrEx;
                                    blist[i].bigexample = example;
                                    blist[i].smallexample = example;
                                    blist[i].sort = j;
                                    blist[i].AnalyticalModel = exdata;
                                    blist[i].AddrVal = j > 3 ? AddressValueEnum.Val : AddressValueEnum.Addr;
                                    if (!string.IsNullOrEmpty(fffd) && !string.IsNullOrEmpty(fffe))
                                    {
                                        blist[i].addresspart = $"{Convert.ToUInt16(fffd, 16).ToString("X2")}{Convert.ToUInt16(fffe, 16).ToString("X2")}";
                                    }
                                    i++;
                                }
                               
                                fff7 -= 2;
                            }

                            if (fff7 <= 0)
                            {
                                f7bool = false;
                                f6bool = false;
                                bigsmall = -1;
                                exdata = string.Empty;
                            }

                        }
                        else
                        {
                            for (int j = 0; j < 4; j++)
                            {
                                blist[i].IsSetting = true;
                                blist[i].bigaddr = bigaddrT;
                                blist[i].smalladdr = smalladdrT;
                                blist[i].bigdata = bigdataT;
                                blist[i].smalldata = smalldataT;
                                blist[i].sort = j;
                                blist[i].AnalyticalModel = exdata;
                                blist[i].AddrVal = j > 1 ? AddressValueEnum.Addr : AddressValueEnum.Val;
                                if (!string.IsNullOrEmpty(fffd) && !string.IsNullOrEmpty(fffe))
                                {
                                    blist[i].addresspart = $"{Convert.ToUInt16(fffd, 16).ToString("X2")}{Convert.ToUInt16(fffe, 16).ToString("X2")}";
                                }
                                i++;
                            }
                           
                        }
                    }
                    else
                    {
                        i += 4;
                    }
                }
                
            }

            if (IsOutput)
            {
                using (StreamWriter writer = new StreamWriter($"{System.IO.Directory.GetCurrentDirectory() }\\Lib\\xc151_Bin\\B_BListSetting.txt"))
                {
                    for (int i = 0; i < blist.Count(); i++)
                    {
                        writer.WriteLine($"原始{blist[i].original} {blist[i].module}_{blist[i].smalladdr}_{blist[i].dataexample}_{blist[i].addresspart}_{blist[i].addrsmallex}");
                    }
                }
            }
            return blist;
        }

        /// <summary>
        /// 设置Isp的解析
        /// </summary>
        /// <param name="blist"></param>
        public static void SetModeIsp(ref List<BinBase> blist) {
            // 获取头文件信息  解析需要解析的内容

            //截取头文件地址信息
            var startHeaderIndex = blist.FindIndex(c =>null !=c.note &&  c.note.Contains("setting区0_00_首地址"));
            var endHeaderIndex = blist.FindLastIndex(c => null != c.note && c.note.Contains("Header_crc"));
            var nums = endHeaderIndex - startHeaderIndex;
           //优化只查找使能开关开的地址
            var  appENList =  blist.Where(c => null != c.note && c.note.Contains("App setting区") && c.note.Contains("_en") && c.original ==1).Select(c=>c.note.Replace("_en","")).Distinct().ToList();
            var result = blist.Skip(startHeaderIndex).Take(nums).Where(c => null != c.note && c.sort == 0x3 && appENList.Any(b=>c.note.Contains(b))).ToList();
            foreach (var item in result)
            {
                if (item.sort == 0x3)
                {
                    //根据地址  找到地址的起始位置   更具5843151  后面的长度*4 计算需要的长度信息
                    var sdata = Convert.ToInt32(item.smalldata, 16);
                    if (blist.Count <=sdata )
                    {
                        continue;
                    }
                    var t = blist[sdata];
                    var t1 = blist[sdata + 4];
                    int count = 0;
                    if (t.smalladdr == "5843"&&t.smalldata == "0151" && t1.smalldata !="0000" )
                    {
                        count = Convert.ToInt32(t1.smalldata, 16) * 4;
                    }
                    //添加过滤无效字段，0000这个字段对IQ文件赋值存在影响
                    for (int i = 0; i < 4; i++)
                    {
                        blist[sdata + 4 + i].IsSetting = false;
                    }
                    var sResult = blist.Skip(sdata).Take(count).ToList();
                   
                    //查找 FFF8 、FFF9 解ISP连写模式
                    for (int j = 0; j < sResult.Count; )
                    {
                        if (j+4<sResult.Count&&sResult[j].smalladdr == "FFF8" )
                        {
                            //FFF8 长度信息  FFF9 地址信息
                            var tlength = Convert.ToInt32( sResult[j].smalldata,16);
                            var oddlegth = tlength % 4; //剩余长度
                            var tAddr = 0;
                            var tIndex = 0;
                            int tempj = 0;

                            for (int h = j; h < j+8; h++)
                            {
                                if (sResult[h].smalladdr == "FFF9")
                                {
                                    tempj = h;
                                    break;
                                }
                            }
                            if (tempj !=0)
                            {
                                tAddr = Convert.ToInt32(sResult[tempj].smalldata, 16);
                                tIndex = blist.IndexOf(sResult[tempj], j) + 4;
                            }


                            //if (sResult[j+1].smalladdr=="FFF9")
                            //{

                            //}
                            //if (sResult[j + 4].smalladdr == "FFF9")
                            //{
                            //    tAddr = Convert.ToInt32(sResult[j + 4].smalldata, 16);
                            //    tIndex = blist.IndexOf(sResult[j + 4], j) + 3;
                            //}

                            if (tAddr >= 0x178D && tAddr <= 0x1798)
                            {

                            }

                            //if (tAddr == 0x0BE0)
                            //{

                            //}
                            int tempAddr = tAddr+ tlength;
                            for (int x = 0; x < tlength - oddlegth; x++)
                            {
                                var addr = ((4 + (x / 4) * 4) - ((x % 4) + 1) + tAddr);
                                blist[tIndex].smalladdr = addr.ToString("X4");
                                blist[tIndex].smalldata = blist[tIndex].original.ToString("X4");
                                blist[tIndex].IsIsp = true;
                                tIndex++;
                                //j++;
                                //tempAddr = addr;
                                if (addr>=0x178D&& addr<=0x1798)
                                {

                                }
                            }
                     
                            if (oddlegth>0)
                            {
                                var oddAddr = tempAddr- oddlegth;  //由于大小端翻转  减一后实际拿到的是是第一个地址位  固需要偏移4位
                                var oddIndex = tIndex + 3;
                               
                                for (int i = 0; i < oddlegth; i++)
                                {
                                    //oddAddr = (Convert.ToUInt32(oddAddr, 16)+1).ToString("X4");
                                    blist[oddIndex].smalladdr = oddAddr.ToString("X4");
                                    blist[oddIndex].smalldata = blist[oddIndex].original.ToString("X4");
                                    blist[oddIndex].IsIsp = true;
                                    oddIndex--;
                                    oddAddr++;
                                    //j++;
                                    if (oddAddr >= 0x178D && oddAddr <= 0x1798)
                                    {

                                    }
                                }
                                //这里需要处理连续地址 这里如果空缺地址  没有则填写FFFF  列如4个地址填写3个地址为缺省的地址 更改成FFFF 确定为空地址
                                for (int i = 0; i < 4 - oddlegth; i++)
                                {
                                    blist[oddIndex].smalladdr = "FFFF";
                                    blist[oddIndex].IsIsp = true;
                                    oddIndex--;
                                }
                            }
                            var tj = j+ (int)Math.Ceiling(tlength / 4.0) * 4 + 8;
                            if (tj>j)
                            {
                                j = tj;
                            }
                            else
                            {
                                j += 4;
                            }
                           //j+= (int)Math.Ceiling(tlength / 4.0) * 4 + 8;


                        }
                        else
                        {
                            j++;
                        }


                        #region 输出打印文件方便查看文件
                        if (count != 0 && IsOutput)
                        {

                            using (StreamWriter writer = new StreamWriter($"{System.IO.Directory.GetCurrentDirectory() }\\Lib\\xc151_Bin\\B_{item.note}.txt"))
                            {
                                if (item.note.Contains("App setting区2_31"))
                                {

                                }
                                for (int i = 0; i < sResult.Count(); i++)
                                {
                                    //if (sResult[i].describe!="crc")
                                    //{
                                    //    writer.WriteLine(sResult[i].smallexample);
                                    //    i += 3;
                                    //}
                                    if (sResult[i].sort == 0x3)
                                    {
                                        writer.WriteLine($"{sResult[i].smallexample},//{sResult[i].addresspart} --{sResult[i].note} --{sResult[i].remarks}");
                                    }
                                }
                            }
                        }
                        #endregion

                    }


                }
            }

            if (IsOutput)
            {
                #region 输出结果打印文件方便查看文件
                using (StreamWriter writer = new StreamWriter($"{System.IO.Directory.GetCurrentDirectory() }\\Lib\\xc151_Bin\\BB_setting.txt"))
                {
                    for (int i = 0; i < blist.Count(); i++)
                    {
                        if (i + 1 < blist.Count() && blist[i].smallexample != blist[i + 1].smallexample)
                        {
                            writer.WriteLine($"{blist[i].smallexample},//{blist[i].addresspart} --{blist[i].note} --{blist[i].remarks} -{blist[i].addrsmallex}");
                        }
                    }
                }
                #endregion
            }


        }


        /// <summary>
        /// 解析 setting区3_31 连接关系地址解析 
        /// </summary>
        /// <param name="blist"></param>
        public static void SetLoadSetting(List<BinBase> blist)
        {

            var tlist =  blist.Where(c =>!string.IsNullOrEmpty(c.note)&& c.note.Contains("setting区3_31_")&&c.smalladdr.Contains("7E")).ToList();
            uint startL = 0x7e00;
            lspList = new List<LoadSettingParse>();
            var tModel = VideoHelper.GetTransferMode();
            var vModel = VideoHelper.GetVideoMode();

            for (int i = 0; i < 49; i++)
            {
                LoadSettingParse model = new LoadSettingParse();
                
                model.SettingHex = Convert.ToUInt16(i);
                model.SettingName = $"setting区{model.SettingHex / 32}_{(model.SettingHex % 32).ToString().PadLeft(2, '0')}";
                //解析setting文件的视频制式 传输模式
               
                if (blist.Exists(c => !string.IsNullOrEmpty(c.note) && c.note.Contains(model.SettingName)))
                {
                    var tblist = blist.Where(c => !string.IsNullOrEmpty(c.note) && c.note.Contains(model.SettingName)).ToList();
                    if (tblist.Exists(c => c.smalladdr.Contains("FFF5")))
                    {
                        var tbIndex = tblist.IndexOf(tblist.First(c => c.smalladdr.Contains("FFF5")));
                        var tbmodel = tblist[tbIndex + 5];
                        if (tbmodel.smalldata !="0000")
                        {
                            model.tHex = Convert.ToUInt32(tbmodel.smalldata.Substring(0, 2), 16);
                            model.tName = tModel.Find(c => c.Hex.Contains(model.tHex.ToString("x2"))).Name;
                            model.vHex = Convert.ToUInt32(tbmodel.smalldata.Substring(2), 16);
                            model.vName = vModel.Find(c => c.Hex.Contains(model.vHex.ToString("x2"))).Name;
                        }
                    }
                   
                }
                model.tveHex =Convert.ToUInt32((tlist.Find(c => c.smalladdr == startL.ToString("X4")) == null ? "0" : tlist.Find(c => c.smalladdr == startL.ToString("X4")).smalldata), 16);
                //根据thex 计算名称
                model.tveName = $"setting区{model.tveHex / 32}_{(model.tveHex % 32).ToString().PadLeft(2,'0')}";
                startL++;
               
                model.IqHex = Convert.ToUInt32((tlist.Find(c => c.smalladdr == (startL).ToString("X4")) == null ? "0" : tlist.Find(c => c.smalladdr == (startL).ToString("X4")).smalldata), 16);
                model.IqName = $"setting区{model.IqHex / 32}_{(model.IqHex % 32).ToString().PadLeft(2, '0')}";
                startL++;
                model.SensorHex = Convert.ToUInt32(tlist.Find(c => c.smalladdr == (startL).ToString("X4")) ==null?"0": tlist.Find(c => c.smalladdr == (startL).ToString("X4")).smalldata, 16);
                model.SensorName = $"setting区{model.SensorHex / 32}_{(model.SensorHex % 32).ToString().PadLeft(2, '0')}";
                startL++;
                lspList.Add(model);
            }
           
        }


        public static bool GetBinName(List<BinBase> blist, out string binName)
        {
            binName = null; // 初始化输出参数

            try
            {
                // 查找偏移地址（假设这个值总是有效的，没有进行错误检查）
                int offsetAddr = Convert.ToInt32(blist[0x368].smalldata, 16);

                // 查找到需要修改的首地址（假设这个条件总是能找到匹配的项）
                var startBin = blist.FirstOrDefault(c => c.note != null && c.note.Contains("App setting区1_31_首地址"));
                if (startBin == null) return false; // 如果没有找到，直接返回false

                int startIndex = Convert.ToInt32(startBin.smalldata, 16);

                // 从首地址开始循环找到特定的字节序列
                for (int i = startIndex; i < blist.Count - 8; i++)
                {
                    if (blist[i].original == 0x42 && blist[i + 1].original == 0x69 &&
                        blist[i + 2].original == 0x6e && blist[i + 3].original == 0x4E &&
                        blist[i + 4].original == 0x61 && blist[i + 5].original == 0x6d &&
                        blist[i + 6].original == 0x65 && blist[i + 7].original == 0x00)
                    {
                        int nameOffset = Convert.ToInt32(
                            $"{blist[i + 11].original:X2}{blist[i + 10].original:X2}{blist[i + 9].original:X2}{blist[i + 8].original:X2}", 16);
                        byte binLength = Convert.ToByte(
                            $"{blist[i + 15].original:X2}{blist[i + 14].original:X2}{blist[i + 13].original:X2}{blist[i + 12].original:X2}", 16);

                        // 计算Bin名称存储地址
                        int binNameIndex = startIndex + nameOffset;

                        // 将Bin名称写入到blist中并转换为字符串
                        byte[] byteData = new byte[binLength];
                        for (int j = 0; j < binLength; j++)
                        {
                            byteData[j] = blist[binNameIndex + j].original;
                        }
                        binName = Encoding.Default.GetString(byteData).TrimEnd('\0');

                        return true;
                    }
                }

                return false; // 如果没有找到匹配的字节序列，返回false
            }
            catch (Exception)
            {
                // 这里可以记录日志或进行其他错误处理，但通常不建议在方法内部捕获所有异常
                return false;
            }
        }




        /// <summary>
        /// 修改头文件中指定位置的值
        /// </summary>
        /// <param name="blist">Bin数据</param>
        /// <param name="Addr">地址</param>
        /// <param name="Val">值</param>
        /// <returns></returns>
        public static bool SetBinUpdateData(ref List<BinBase> blist, string Addr, string Val)
        {
            try
            {
                //查找偏移地址
                var Offsetaddr = Convert.ToInt32(blist[0x368].smalldata,16);
                //查找到需要修改的首地址
                var BIndex = blist.FindLastIndex(c => null != c.describe && c.describe.Contains("BIN密码的地址"));
                var tempUint = Convert.ToUInt32(Val, 16);
                byte[] bTemp = new byte[4];
                bTemp[0] = (byte)(tempUint & 0xff);
                bTemp[1] = (byte)(tempUint >> 8 & 0xff);
                bTemp[2] = (byte)(tempUint >> 16 & 0xff);
                bTemp[3] = (byte)(tempUint >> 24 & 0xff);
                blist[BIndex].original = bTemp[0];
                blist[BIndex - 1].original = bTemp[1];
                blist[BIndex - 2].original = bTemp[2];
                blist[BIndex - 3].original = bTemp[3];
                var HeaderList = blist.Where(c => null != c.module && c.module.Contains("Header")).ToList();
                //重新计算CRC
                Byte[] crcList = new Byte[HeaderList.Count() - 4]; 
                //-4去掉原本CRC
                for (int i = 0; i < crcList.Count(); i++)
                {
                    crcList[i] = HeaderList[i].original;
                }
                var crcData = XchipCrcCheck16(0xFFFF, crcList);
                bTemp = new byte[2];
                bTemp[0] = (byte)(crcData & 0xff);
                bTemp[1] = (byte)(crcData >> 8 & 0xff);
                //修改crc
                var aasd = blist.Where(c => null != c.note && c.note.Contains("Header_crc")).ToList();
                var HCrcIndex = blist.FindLastIndex(c => null != c.note && c.note.Contains("Header_crc"));
                blist[HCrcIndex - 3].original = bTemp[0];
                blist[HCrcIndex - 2].original = bTemp[1];
                return true;
            }
            catch (Exception)
            {
                return false;
                throw;
            }

        }


        /// <summary>
        /// 根据Bin内容，SettingFlag 更改传进来的地址和值进行修改处理工作
        /// </summary>
        /// <param name="blist">Bin数据</param>
        /// <param name="Addr">地址</param>
        /// <param name="Val">值</param>
        /// <param name="settingflag">Setting的值</param>
        /// <returns></returns>
        public static bool SetBinUpdateData(ref List<BinBase> blist, string Addr, string Val, uint settingflag)
        {
            try
            {
                //根据SettingFlag获取文件的链接关系
                var lspModel = lspList.Find(c => c.SettingHex == settingflag);
                //根据链接关系获取对应数据查找数据
                var lspTempList = blist.Where(c => null != c.note && (c.note.Contains(lspModel.SettingName) || c.note.Contains(lspModel.tveName) || c.note.Contains(lspModel.IqName) || c.note.Contains(lspModel.SensorName))).ToList();
                var sList = lspTempList.Where(c => c.addrsmallex.ToLower().Contains(Addr.ToLower())).ToList();
                if (sList.Count <= 0)
                {
                    logger.Debug($"未查找到地址：0x{Addr} 查找范围 -setting-{lspModel.SettingName} -tve-{lspModel.tveName} -Iq-{lspModel.IqName}  -Sensor-{lspModel.SensorName}");
                    //未找到对应寄存器
                    return false;
                }
                var index = blist.IndexOf(sList.Last());
                if (index == 0)
                {
                    return false;
                }
                //获取Crc位置信息  为4个字节  
                var CrcIndex = 0;
                for (int i = index; i > 0; i--)
                {
                    if (blist[i].smalladdr.ToLower() == "f6f5")
                    {
                        CrcIndex = i;
                        break;
                    }
                }
                var tempUint = Convert.ToUInt32(Val, 16);
                var bModel = blist[index];

                if (null == UpdateIndex)
                {
                    UpdateIndex = new List<int>();
                }
                //记录更新的的索引
                UpdateIndex.Add(index);

                if (bModel.IsIsp == true)
                {
                    logger.Debug($"更新索引：{index}  地址：{Addr} 原始值：{ blist[index].original.ToString("X")}  更新值 ：{tempUint.ToString("X")}");
                    blist[index].original = (byte)tempUint;
                    blist[index].smalldata = tempUint.ToString("X4");

                }
                else
                {
                    var sortList = sList.OrderBy(c => c.sort).Select(c => c.sort).Distinct().ToList();
                    if (sortList.Count == 4)
                    {
                        byte[] bTemp = new byte[2];
                        bTemp[0] = (byte)(tempUint & 0xff);
                        bTemp[1] = (byte)(tempUint >> 8 & 0xff);
                        var bIndex = blist.FindLastIndex(c => c.addrsmallex.Contains(bModel.addrsmallex) && c.note.Contains(bModel.note) && c.AddrVal == AddressValueEnum.Val);
                        //01 为数据  23为地址   针对当前Bin 数据  分大端和小端   23 为数据  01为地址   注释的是判断高低位的
                        //blist[index - 1].original = bTemp[1];
                        //blist[index].original = bTemp[0];
                        logger.Debug($"更新索引：{index}  地址：{Addr} 原始值：{  blist[index - 1].original.ToString("X")}{blist[index].original.ToString("X")}  更新值 ：{tempUint.ToString("X")}");
                        blist[bIndex - 1].original = bTemp[0];
                        blist[bIndex].original = bTemp[1];
                        //UpdateIndex.Add(index);
                    }
                    else if (sortList.Count == 8)
                    {
                        byte[] bTemp = new byte[4];
                        bTemp[0] = (byte)(tempUint & 0xff);
                        bTemp[1] = (byte)(tempUint >> 8 & 0xff);
                        bTemp[2] = (byte)(tempUint >> 16 & 0xff);
                        bTemp[3] = (byte)(tempUint >> 24 & 0xff);
                        var BIndex = blist.FindLastIndex(c => c.addrsmallex.Contains(bModel.addrsmallex) && c.note.Contains(bModel.note) && c.AddrVal == AddressValueEnum.Val);
                        logger.Debug($"更新索引：{index}  地址：{Addr} 原始值：{  blist[index].original.ToString("X")}{ blist[index - 1].original.ToString("X")}{blist[index - 2].original.ToString("X") }{blist[index - 3].original.ToString("X")}  更新值 ：{tempUint.ToString("X")}");
                        blist[BIndex].original = bTemp[3];
                        blist[BIndex - 1].original = bTemp[2];
                        blist[BIndex - 2].original = bTemp[1];
                        blist[BIndex - 3].original = bTemp[0];
                        //UpdateIndex.Add(index);
                    }
                }



                //更具Crc位置  获取后面128个字节数据 进行Crc的重新计算 并赋值
                Byte[] crcList = new Byte[256]; //修改成256
                for (int i = 0; i < crcList.Count(); i++)
                {
                    crcList[i] = blist[CrcIndex + i + 1].original;
                }
                //重新计算Crc
                var crcData = XchipCrcCheck16(0xFFFF, crcList);
                //赋值Crc
                if (blist[CrcIndex].original == 0xf6 && blist[CrcIndex - 1].original == 0xf5) //修改成f6f5
                {
                    logger.Debug($"CRC更新索引：{CrcIndex}  地址：0x{Addr} 原始值：{ blist[CrcIndex - 3].original.ToString("X")}{ blist[CrcIndex - 2].original.ToString("X") }  更新值 ：{crcData.ToString("X")}");
                    blist[CrcIndex - 3].original = (byte)(crcData & 0xff);
                    blist[CrcIndex - 2].original = (byte)(crcData >> 8 & 0xff);
                }


                return true;
            }
            catch (Exception ex)
            {
                logger.Debug(ex);
                throw;
            }
            
        }


        public static bool SstBinUsbUpdateData(ref List<BinBase> blist, string Addr, string Val,int _width,int _height)
        {
            try
            {
                //根据宽高 找到解析里面的内容  这里只做IQ_Config 和IQ_Common  处理  ISP_Config 、ISP_Common 、Sensor_Common 、Sensor_Config 的放到后期处理
                var list = usbList.Where(c => (c.width == _width && c.height == _height && c.Type == SettingFileType.IQ_Config) || c.Type == SettingFileType.IQ_Common).ToList();
                //IQ_Common,  //setting区2_    setting区2_00
                //IQ_Config,  //setting区2_
                //PLL,        //setting区3_
                //ISP_Common,  //setting区3_
                //ISP_Config,  //setting区3_
                //Sensor_Config,  //setting区3_
                //Sensor_Common,  //setting区3_   
                var msg = string.Empty;
                //根据链接关系获取对应数据查找数据  拿到存放位置信息
                List<BinBase> lspTempList = new List<BinBase>();
                foreach (var item in list)
                {
                    lspTempList.AddRange(blist.Where( c=>null != c.note && c.note.Contains(item.note)));
                    msg +=$"查找范围Setting：{item.Type.ToString()} 查找范围 {item.note} ";
                }

                //var lspTempList = blist.Where(c => null != c.note && (c.note.Contains(lspModel.SettingName) || c.note.Contains(lspModel.tveName) || c.note.Contains(lspModel.IqName) || c.note.Contains(lspModel.SensorName))).ToList();

                var sList = lspTempList.Where(c => c.addrsmallex.ToLower().Contains(Addr.ToLower())).ToList();
                if (sList.Count <= 0)
                {
                    logger.Debug($"未查找到地址：0x{Addr} 查找范围  {msg}");
                    //未找到对应寄存器
                    return false;
                }
                var index = blist.IndexOf(sList.Last());
                if (index == 0)
                {
                    return false;
                }
                //获取Crc位置信息  为4个字节  
                var CrcIndex = 0;
                for (int i = index; i > 0; i--)
                {
                    if (blist[i].smalladdr.ToLower() == "f6f5")
                    {
                        CrcIndex = i;
                        break;
                    }
                }
                var tempUint = Convert.ToUInt32(Val, 16);
                var bModel = blist[index];

                if (null == UpdateIndex)
                {
                    UpdateIndex = new List<int>();
                }
                //记录更新的的索引
                UpdateIndex.Add(index);

                if (bModel.IsIsp == true)
                {
                    logger.Debug($"更新索引：{index}  地址：{Addr} 原始值：{ blist[index].original.ToString("X")}  更新值 ：{tempUint.ToString("X")}");
                    blist[index].original = (byte)tempUint;
                    blist[index].smalldata = tempUint.ToString("X4");

                }
                else
                {
                    var sortList = sList.OrderBy(c => c.sort).Select(c => c.sort).Distinct().ToList();
                    if (sortList.Count == 4)
                    {
                        byte[] bTemp = new byte[2];
                        bTemp[0] = (byte)(tempUint & 0xff);
                        bTemp[1] = (byte)(tempUint >> 8 & 0xff);
                        var bIndex = blist.FindLastIndex(c => c.addrsmallex.Contains(bModel.addrsmallex) && c.note.Contains(bModel.note) && c.AddrVal == AddressValueEnum.Val);
                        //01 为数据  23为地址   针对当前Bin 数据  分大端和小端   23 为数据  01为地址   注释的是判断高低位的
                        //blist[index - 1].original = bTemp[1];
                        //blist[index].original = bTemp[0];
                        logger.Debug($"更新索引：{index}  地址：{Addr} 原始值：{  blist[index - 1].original.ToString("X")}{blist[index].original.ToString("X")}  更新值 ：{tempUint.ToString("X")}");
                        blist[bIndex - 1].original = bTemp[0];
                        blist[bIndex].original = bTemp[1];
                        //UpdateIndex.Add(index);
                    }
                    else if (sortList.Count == 8)
                    {
                        byte[] bTemp = new byte[4];
                        bTemp[0] = (byte)(tempUint & 0xff);
                        bTemp[1] = (byte)(tempUint >> 8 & 0xff);
                        bTemp[2] = (byte)(tempUint >> 16 & 0xff);
                        bTemp[3] = (byte)(tempUint >> 24 & 0xff);
                        var BIndex = blist.FindLastIndex(c => c.addrsmallex.Contains(bModel.addrsmallex) && c.note.Contains(bModel.note) && c.AddrVal == AddressValueEnum.Val);
                        logger.Debug($"更新索引：{index}  地址：{Addr} 原始值：{  blist[index].original.ToString("X")}{ blist[index - 1].original.ToString("X")}{blist[index - 2].original.ToString("X") }{blist[index - 3].original.ToString("X")}  更新值 ：{tempUint.ToString("X")}");
                        blist[BIndex].original = bTemp[3];
                        blist[BIndex - 1].original = bTemp[2];
                        blist[BIndex - 2].original = bTemp[1];
                        blist[BIndex - 3].original = bTemp[0];
                        //UpdateIndex.Add(index);
                    }
                }



                //更具Crc位置  获取后面128个字节数据 进行Crc的重新计算 并赋值
                Byte[] crcList = new Byte[256]; //修改成256
                for (int i = 0; i < crcList.Count(); i++)
                {
                    crcList[i] = blist[CrcIndex + i + 1].original;
                }
                //重新计算Crc
                var crcData = XchipCrcCheck16(0xFFFF, crcList);
                //赋值Crc
                if (blist[CrcIndex].original == 0xf6 && blist[CrcIndex - 1].original == 0xf5) //修改成f6f5
                {
                    logger.Debug($"CRC更新索引：{CrcIndex}  地址：0x{Addr} 原始值：{ blist[CrcIndex - 3].original.ToString("X")}{ blist[CrcIndex - 2].original.ToString("X") }  更新值 ：{crcData.ToString("X")}");
                    blist[CrcIndex - 3].original = (byte)(crcData & 0xff);
                    blist[CrcIndex - 2].original = (byte)(crcData >> 8 & 0xff);
                }


            }
            catch (Exception ex)
            {
                logger.Debug(ex);
                throw;
            }

            return true;
        }

        /// <summary>
        /// 根据内容  生成出Bin文件
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="blist">全局bin内容</param>
        public static void BinOutFile(string path, List<BinBase> blist) {

            using (FileStream fileStream = new FileStream(path, FileMode.Create))
            {
                foreach (var number in blist)
                {
                    byte[] bytes = BitConverter.GetBytes(number.original);
                    fileStream.Write(bytes, 0, 1);
                }
            }
        }


        #region 废除 更新地址
        ///// <summary>
        ///// 根据地址 查找对应的Bin位置信息  并赋值
        ///// </summary>
        ///// <param name="blist">原始数据</param>
        ///// <param name="Addr">地址</param>
        ///// <param name="Val">值</param>
        ///// <param name="isLog">是否启动日志</param>
        ///// <param name="module">区域信息</param>
        ///// <returns>原始数据</returns>
        //public static List<BinBase> SetBinBaseToOriginal(List<BinBase> blist, string Addr, string Val, string module, bool isLog = false, int ModeEnd = 0)
        //{

        //    //查找blist 值的位置
        //    var addrList = blist.Where(c => c.addrsmallex.ToLower().Contains(Addr.ToLower()) && c.note.Contains(module)).ToList();

        //    //addrList = blist.Where(c => c.addrsmallex.ToLower().Contains("80000001") && c.module.Contains(module)).ToList();
        //    //var aaaa = blist.Where(c => c.module!=null && c.module.Contains(module)).ToList();
        //    var sortList = addrList.OrderBy(c => c.sort).Select(c => c.sort).Distinct().ToList();
        //    //var addrsIndex = blist.FindIndex(c => c.addrsmallex.Contains(Addr) && c.module.Contains(module));
        //    var addreIndex = blist.FindLastIndex(c => c.addrsmallex.ToLower().Contains(Addr.ToLower()) && c.note.Contains(module));
        //    //获取Crc位置信息  为4个字节  
        //    var CrcIndex = 0;
        //    for (int i = addreIndex; i > 0; i--)
        //    {
        //        if (blist[i].smalladdr.ToLower() == "f6f5")
        //        {
        //            CrcIndex = i;
        //            break;
        //        }
        //    }

        //    logger.Debug($"旧的Crc:{blist[CrcIndex].original.ToString("X")}{blist[CrcIndex - 1].original.ToString("X")}{blist[CrcIndex - 2].original.ToString("X")}{blist[CrcIndex - 3].original.ToString("X")}");
        //    string filePath = string.Empty;
        //    if (isLog)
        //    {
        //        filePath = $"{System.AppDomain.CurrentDomain.BaseDirectory}\\Lib\\Log_{DateTime.Now.ToString("MMddyyyyHH")}.txt";

        //        using (StreamWriter writer = new StreamWriter(filePath, false))
        //        {


        //            // 写入内容  
        //            writer.WriteLine($"旧的Crc:{blist[CrcIndex].original.ToString("X")}{blist[CrcIndex - 1].original.ToString("X")}{blist[CrcIndex - 2].original.ToString("X")}{blist[CrcIndex - 3].original.ToString("X")}");
        //            writer.WriteLine("旧的Bin数据:");
        //            //Byte[] crcListold = new Byte[128];
        //            for (int i = 0; i < 256; i++) //修改成256
        //            {
        //                writer.Write(blist[CrcIndex + i + 1].original.ToString("X2"));
        //                writer.Write(blist[CrcIndex + i + 2].original.ToString("X2"));
        //                writer.Write(blist[CrcIndex + i + 3].original.ToString("X2"));
        //                writer.WriteLine(blist[CrcIndex + i + 4].original.ToString("X2"));
        //                i += 3;
        //            }

        //        }
        //    }


        //    /*
        //    关于地址和值处理方式说明：
        //    1.0找到多个相同寄存器的值  只取最后一条数据进行修改  前面部分不进行数据修改 
        //    */
        //    if (sortList.Count() == 4)
        //    {

        //        //01 为数据  23为地址   针对当前Bin 数据  分大端和小端   23 为数据  01为地址
        //        var tempUint = Convert.ToUInt32(Val, 16);
        //        byte[] bTemp = new byte[2];
        //        if (ModeEnd == 0)
        //        {
        //            bTemp[0] = (byte)(tempUint & 0xff);
        //            bTemp[1] = (byte)(tempUint >> 8 & 0xff);
        //        }
        //        else
        //        {
        //            bTemp[1] = (byte)(tempUint & 0xff);
        //            bTemp[0] = (byte)(tempUint >> 8 & 0xff);
        //        }
        //        // 找到索引 赋值数据
        //        //var indexL = blist.FindLastIndex(c => c.addrsmallex.ToLower().Contains(Addr.ToLower()) && c.module.Contains(module) && c.sort == 2);
        //        //var indexH = blist.FindLastIndex(c => c.addrsmallex.ToLower().Contains(Addr.ToLower()) && c.module.Contains(module) && c.sort == 3);

        //        //blist[indexL].original = bTemp[0];
        //        //blist[indexH].original = bTemp[1];


        //        var index = blist.FindLastIndex(c => c.addrsmallex.ToLower().Contains(Addr.ToLower()) && c.note.Contains(module) && c.AddrVal == AddressValueEnum.Val);
        //        blist[index - 1].original = bTemp[0];
        //        blist[index].original = bTemp[1];
        //    }
        //    else if (sortList.Count() == 8)
        //    {
        //        //0123为数据  4567为地址 针对当前Bin 数据  分大端和小端   4567为数据  0123为地址
        //        var tempUint = Convert.ToUInt32(Val, 16);
        //        byte[] bTemp = new byte[4];
        //        if (ModeEnd == 0)
        //        {
        //            bTemp[0] = (byte)(tempUint & 0xff);
        //            bTemp[1] = (byte)(tempUint >> 8 & 0xff);
        //            bTemp[2] = (byte)(tempUint >> 16 & 0xff);
        //            bTemp[3] = (byte)(tempUint >> 24 & 0xff);
        //        }
        //        else
        //        {
        //            bTemp[3] = (byte)(tempUint & 0xff);
        //            bTemp[2] = (byte)(tempUint >> 8 & 0xff);
        //            bTemp[1] = (byte)(tempUint >> 16 & 0xff);
        //            bTemp[0] = (byte)(tempUint >> 24 & 0xff);
        //        }
        //        //var indexL = blist.FindLastIndex(c => c.addrsmallex.ToLower().Contains(Addr.ToLower()) && c.module.Contains(module) && c.sort == 4);
        //        //var indexM0 = blist.FindLastIndex(c => c.addrsmallex.ToLower().Contains(Addr.ToLower()) && c.module.Contains(module) && c.sort == 5);
        //        //var indexM1 = blist.FindLastIndex(c => c.addrsmallex.ToLower().Contains(Addr.ToLower()) && c.module.Contains(module) && c.sort == 6);
        //        //var indexH = blist.FindLastIndex(c => c.addrsmallex.ToLower().Contains(Addr.ToLower()) && c.module.Contains(module) && c.sort == 7);


        //        //blist[indexL].original = bTemp[0];
        //        //blist[indexM0].original = bTemp[1];
        //        //blist[indexM1].original = bTemp[2];
        //        //blist[indexH].original = bTemp[3];


        //        var index = blist.FindLastIndex(c => c.addrsmallex.ToLower().Contains(Addr.ToLower()) && c.note.Contains(module) && c.AddrVal == AddressValueEnum.Val);
        //        blist[index].original = bTemp[3];
        //        blist[index - 1].original = bTemp[2];
        //        blist[index - 2].original = bTemp[1];
        //        blist[index - 3].original = bTemp[0];


        //    }
        //    else
        //    {
        //        //异常数据

        //    }
        //    /*
        //    Crc 处理方式说明：
        //    1.不对FFF5的Crc进行更改 这里不需要更改  属于文件头信息
        //    2.只针对F5F5 里面的是数据进行更改
        //    3.遇到找不到F5F5的情况  可能情况寄存器查找错误 需要处理
        //    4.Crc从F5F5 后的寄存器128个字节进行处理 
        //    */

        //    //var CrcIndex = blist.FindIndex(c => c.addrsmallex.Contains(Addr) && c.module.Contains(module));

        //    //判断crc位置信息 这里是错误处理
        //    if (addreIndex - CrcIndex > 128)
        //    {

        //    }
        //    //更具Crc位置  获取后面128个字节数据 进行Crc的重新计算 并赋值
        //    Byte[] crcList = new Byte[256]; //修改成256
        //    for (int i = 0; i < crcList.Count(); i++)
        //    {
        //        crcList[i] = blist[CrcIndex + i + 1].original;
        //    }
        //    //重新计算Crc
        //    var crcData = XchipCrcCheck16(0xFFFF, crcList);
        //    //赋值Crc
        //    if (blist[CrcIndex].original == 0xf6 && blist[CrcIndex - 1].original == 0xf5) //修改成f6f5
        //    {
        //        blist[CrcIndex - 3].original = (byte)(crcData & 0xff);
        //        blist[CrcIndex - 2].original = (byte)(crcData >> 8 & 0xff);
        //    }
        //    if (isLog && !string.IsNullOrEmpty(filePath))
        //    {
        //        //filePath = $"{System.AppDomain.CurrentDomain.BaseDirectory}\\Lib\\Log_{DateTime.Now.ToString("MMddyyyyHH")}.txt";
        //        //using (StreamWriter writer = new StreamWriter(filePath, false))
        //        //{
        //        //    // 写入内容  
        //        //    writer.WriteLine($"新的Crc:{blist[CrcIndex].original}{blist[CrcIndex - 1].original}{blist[CrcIndex - 2].original}{blist[CrcIndex - 3].original}");
        //        //    writer.WriteLine("新的Bin数据:");
        //        //    //Byte[] crcListold = new Byte[128];
        //        //    for (int i = 0; i < 128; i++)
        //        //    {
        //        //        writer.Write(blist[CrcIndex + i + 1].original);
        //        //    }

        //        //}
        //        filePath = $"{System.AppDomain.CurrentDomain.BaseDirectory}\\Lib\\Log2_{DateTime.Now.ToString("MMddyyyyHH")}.txt";

        //        using (StreamWriter writer = new StreamWriter(filePath, false))
        //        {


        //            // 写入内容  
        //            writer.WriteLine();
        //            writer.WriteLine($"新的Crc:{blist[CrcIndex].original.ToString("X")}{blist[CrcIndex - 1].original.ToString("X")}{blist[CrcIndex - 2].original.ToString("X")}{blist[CrcIndex - 3].original.ToString("X")}");
        //            writer.WriteLine("新的Bin数据:");
        //            //Byte[] crcListold = new Byte[128];
        //            for (int i = 0; i < 256; i++) //修改成256
        //            {
        //                writer.Write(blist[CrcIndex + i + 1].original.ToString("X2"));
        //                writer.Write(blist[CrcIndex + i + 2].original.ToString("X2"));
        //                writer.Write(blist[CrcIndex + i + 3].original.ToString("X2"));
        //                writer.WriteLine(blist[CrcIndex + i + 4].original.ToString("X2"));
        //                i += 3;
        //            }

        //        }
        //        //using (FileStream fs = File.Open(filePath, FileMode.Append))
        //        //{
        //        //    using (StreamWriter writer = new StreamWriter(fs))
        //        //    {
        //        //        // 写入内容  
        //        //        writer.WriteLine();
        //        //        writer.WriteLine($"新的Crc:{blist[CrcIndex].original.ToString("X")}{blist[CrcIndex - 1].original.ToString("X")}{blist[CrcIndex - 2].original.ToString("X")}{blist[CrcIndex - 3].original.ToString("X")}");
        //        //        writer.WriteLine("新的Bin数据:");
        //        //        //Byte[] crcListold = new Byte[128];
        //        //        for (int i = 0; i < 128; i++)
        //        //        {
        //        //            writer.WriteLine(blist[CrcIndex + i + 1].original.ToString("X2"));
        //        //        }
        //        //    }
        //        //}
        //    }

        //    return blist;
        //}

        #endregion


        public  static UInt16 XchipCrcCheck16(UInt16 Input, byte[] data)
        {
            UInt16 crc_reg = Input;
            for (int i = 0; i < data.Count(); i++)
            {
                var tempCount = (crc_reg ^ data[i]) & 0xff;
                crc_reg = Convert.ToUInt16((crc_reg >> 8) ^ crc16_table[tempCount]);
            }
            return Convert.ToUInt16(crc_reg & 0xFFFF);
        }



        /// <summary>
        /// crc 全局变量
        /// </summary>
        static UInt16[] crc16_table ={
          0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
          0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
          0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
          0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
          0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
          0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
          0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
          0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
          0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
          0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
          0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
          0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
          0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
          0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
          0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
          0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
          0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
          0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
          0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
          0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
          0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
          0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
          0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
          0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
          0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
          0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
          0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
          0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
          0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
          0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
          0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
          0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78,
          };




    }



    //public class BigBinBaseCompare : IEqualityComparer<BinBase>
    // {
    //     public bool Equals(BinBase x, BinBase y)
    //     {
    //         return x.bigexample == y.bigexample;
    //     }

    //     public int GetHashCode(BinBase obj)
    //     {
    //         return obj.bigexample.GetHashCode();
    //     }
    // }
    public class SmallBinBaseCompare : IEqualityComparer<BinBase>
    {
        public bool Equals(BinBase x, BinBase y)
        {
            return x.smallexample == y.smallexample;
        }

        public int GetHashCode(BinBase obj)
        {
            return obj.smallexample.GetHashCode();
        }
    }
}
